#ifndef CURLTRANSPORT_H
#define CURLTRANSPORT_H

#ifdef WX_PRECOMP
#include "wx/wx.h"
#endif

#include <list>

#include <wx/string.h>
#include <wx/tokenzr.h>

#include <curl/curl.h>

#include "tux.h"
#include "MsnTransport.h"
 

class DataHolder
{
public:
	char* data;
	int length;

public:
	DataHolder(char* buf, int len)
	{
		length = len;
		data = new char[len];

		memcpy(data, buf, len);

	};
	~DataHolder(){delete[] data;}

};

class CURLTransport : public MsnTransport
{
typedef std::list<DataHolder*> DataList;

private:
	wxString gatewayIP;
	wxString sessionID;

	CURL *curl;

	DataList dataList;

	wxMutex dataMutex;

public:
	CURLTransport();
    virtual ~CURLTransport();

public:
    virtual void* Entry();

	virtual void SendData(void* data, unsigned int size);

	virtual bool Open();
	virtual void Close();
	
protected:
	bool OpenHttp();

	bool ExitHttp();
	bool PollHttp();
	bool PostHttp(const wxString& url, unsigned char* bodyBuf, unsigned int bodySize, wxMemoryBuffer& respHeader, wxMemoryBuffer& respBody);

	void SendDataImpl(void* data, unsigned int size);

protected:
	void ProcessHttpHeader(wxMemoryBuffer& buf);
	void ProcessHttpBody(wxMemoryBuffer& buf);

	void SendWaitingData();

	static size_t HttpWrite_Callback(void *ptr, size_t size, size_t nmemb, void *stream);

	wxString GetSessionID(const wxString& headers);
	wxString GetGatewayIP(const wxString& headers);
	int GetContentLength(const wxString& headers);
};

#endif
